#ifndef __SPI_FRAMEWORK_H
#define __SPI_FRAMEWORK_H

#include "../GenericTypeDefs.h"
#include "xc.h"

#define  SPI_DataBuf_LEN   18
#define  WRITE_DATA_SIZE  68  // cmd + len + 66 data
#define  READ_DATA_SIZE   6   // 6 byte 

typedef struct _SSS_MSG_STRUCT {
    unsigned char Head;            // FH(frame header)
    unsigned char Cmd;             // CMD
    unsigned char Len;
    unsigned char WData[SPI_DataBuf_LEN];       // data
    unsigned char RData[4];        // data
    unsigned char WCheckSum;       // checksum
    unsigned char RCheckSum;       // checksum
    unsigned char RW;              // 
} SSS_MSG_STRUCT;
extern SSS_MSG_STRUCT    SPI_Msg;

typedef struct _SPI_WORK_MSG_STRUCT {
    uint8_t  Read_En;        // Read Enable
    uint8_t  Data[20];       // Data: up to 16 bytes. Read version number: Start + (cmd + data (4 bytes) + CRC (2 bytes)) * 2, possibly escaped + End.
    uint8_t  Len;            // length
    uint16_t DlayValue;      // delay time
    uint8_t  Read_En_ck;
    uint8_t  Cmd;
} SPI_WORK_MSG_STRUCT;
extern SPI_WORK_MSG_STRUCT  SPI_WorkMsg;

typedef struct _NEW_SPI_MSG_STRUCT {
    unsigned char WData[WRITE_DATA_SIZE];       // data
    unsigned char RData[READ_DATA_SIZE];        // data
    unsigned char Len;
    unsigned char RW;
} NEW_SPI_MSG_STRUCT;
extern  NEW_SPI_MSG_STRUCT  New_SPI_MSG;

extern void SPI_WriteInstruction(uint8_t slaveNum, NEW_SPI_MSG_STRUCT* pDat);
extern uint8_t SPI_ReadInstruction(uint8_t slaveNum, NEW_SPI_MSG_STRUCT* pDat);
void QIXIN_EnterBoot(uint8_t slaveNum);
void SPIEventHandler(uintptr_t context );
void SlaveSelect(uint8_t slaveNum);
#endif